package com.creditease.cn.monitor.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.creditease.cn.monitor.dao.redis.SlaveRedisNode;
import com.creditease.cn.monitor.dto.CommandKeyDTO;
import com.creditease.cn.monitor.service.CommandKeyService;

@Service
public class CommandKeyServiceImpl implements CommandKeyService{
	@Resource
	private SlaveRedisNode slaveRedisNode;
	
	public Set<CommandKeyDTO> keys(String pattern) {
		Set<CommandKeyDTO> commandKeyDtos = new HashSet<CommandKeyDTO>();
		Jedis jedis = slaveRedisNode.getConnection();
		Set<String> keys = jedis.keys(pattern);
		for(String key : keys){
			String type = jedis.type(key);
			long ttl = jedis.ttl(key);
			
			CommandKeyDTO dto = new CommandKeyDTO();
			dto.setKey(key);
			dto.setType(type);
			dto.setTtl(ttl);
			commandKeyDtos.add(dto);
		}
		slaveRedisNode.close(jedis);
		return commandKeyDtos;
	}

	
	public List<String> values(String key){
		System.out.println("key" + key);
		Jedis jedis = slaveRedisNode.getConnection();
		System.out.println(jedis);
		Set<String> list = jedis.smembers(key);
		System.out.println(list.size());
		for(String value : list){
			System.out.println(value);
		}
		slaveRedisNode.close(jedis);
//		return list;
		return null;
	}
	
	
}
